package com.leo.biz.controller;

import com.leo.biz.service.datacenter.CommServiceImpl;
import com.leo.biz.service.datakeep.DataCommKeeper;
import com.leo.biz.service.datakeep.FetchTaskManager;
import com.leo.biz.service.datakeep.FileReadServiceImpl;
import com.leo.biz.service.fetcher.BaseStockFetcher;

import com.leo.biz.service.impl.Alog;
import com.leo.biz.service.pub.DaoCenter;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by leo on 2016/1/29.
 */
@Controller
@RequestMapping(value = "/auth")
public class LoginController {

    @Autowired
    private Alog alog;

    @Autowired
    @Qualifier("txStockFetcher")
    private BaseStockFetcher txStockFetcher;

    @Autowired
    private FileReadServiceImpl fileReadService;

    @Autowired
    private DaoCenter daoCenter;

    @Autowired
    private FetchTaskManager fetchTaskManager;

    @Autowired
    private  DataCommKeeper dataCommKeeper;

    @Autowired
    private CommServiceImpl commService;




    @RequestMapping(value="/login", method= RequestMethod.GET,produces = "application/json; charset=utf-8")

    public Object login(HttpServletRequest request)
    {

        String username=request.getParameter("username");
        String password=request.getParameter("password");
        ModelAndView modelAndView=new ModelAndView();
        if(username==null || password==null || username.equalsIgnoreCase("") || password.equalsIgnoreCase(""))
        {
            modelAndView.setViewName("login");
            return  modelAndView;
        }

        Subject currentUser = SecurityUtils.getSubject();
         String newPassword = new SimpleHash(
            "md5",
                 password,
            ByteSource.Util.bytes(username+"8d78869f470951332959580424d4bf4f")).toHex();
        UsernamePasswordToken token = new UsernamePasswordToken(
                username,newPassword);
        token.setRememberMe(false);
        try {
            currentUser.login(token);

            modelAndView.setViewName("stock/index");
            modelAndView.addObject("username", username);
            alog.log("账号登录成功:"+username);
        } catch (AuthenticationException e) {

            e.printStackTrace();
            alog.log("账号登录失败:"+username);

            modelAndView.setViewName("login");

        }

        return modelAndView;
    }



    @RequestMapping(value="/logout", method= RequestMethod.GET,produces = "application/json; charset=utf-8")
    @ResponseBody
    public Object logout(HttpSession session,HttpServletRequest request)
    {

        Subject currentUser = SecurityUtils.getSubject();

        try {
            currentUser.logout();


        } catch (AuthenticationException e) {

            e.printStackTrace();


        }

        return "Logout";
    }

}

